<?php
/*
 * @Date: 2025-04-12 23:51:29
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-04-12 23:56:39
 * @FilePath: /admin/app/Http/Controllers/PermissionController.php
 */

namespace App\Http\Controllers;

use App\Http\Requests\StorePermissionRequest;
use App\Http\Requests\UpdatePermissionRequest;
use App\Http\Resources\PermissionResource;
use App\Models\Permission;

class PermissionController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $permissions = Permission::latest()
            ->key(request('key'))
            ->paginate(request('per_page', 15));

        return PermissionResource::collection($permissions);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StorePermissionRequest $request, Permission $permission)
    {
        $permission->fill($request->input() + ['guard_name' => 'sanctum'])->save();

        return $this->success('添加成功', data: $permission);
    }

    /**
     * Display the specified resource.
     */
    public function show(Permission $permission)
    {
        return $this->success(data: new PermissionResource($permission));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdatePermissionRequest $request, Permission $permission)
    {
        $permission->fill($request->input())->save();

        return $this->success('更新成功');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Permission $permission)
    {
        $permission->delete();

        return $this->success(message: '删除成功');
    }
}
